package com.pwc.conversionTool.rest;



import java.io.IOException;

import javax.servlet.ServletContext;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.Manager.SoapUIManager;
import com.pwc.conversionTool.config.impl.XMLConfigImpl;




@Controller
public class XMLConfigRest {
	
	private static final Logger logger = LoggerFactory.getLogger(XMLConfigRest.class);
	

	@Autowired
	public ServletContext context;
	
	/**
	 * get the config file data
	 * @return 
	 */
	@RequestMapping(value = "/rest/config/xmlConfigData", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody String getXMLConfig() {
		
		String xmlData="";
		try {
			XMLConfigImpl configImpl=XMLConfigImpl.initial(context);
			xmlData=configImpl.getXMLString();
		} catch (Exception e) {
			logger.error("initial error");
		}
		return xmlData;
	}
	
	/**
	 * save data
	 * @return
	 */
	@RequestMapping(value = "/rest/config/SaveXmlData", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody void setXMLConfig(String xmlData) {
		
		try {
			XMLConfigImpl configImpl=XMLConfigImpl.initial(context);
			configImpl.saveDocument(xmlData);
		} catch (Exception e) {
			logger.error("initial error");
		}
		
		
	}
	
	
	/**
	 * connect to endpoint and save to config
	 * @param endpoint
	 * @return
	 */
	@RequestMapping(value = "/rest/config/endpoint", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	   public @ResponseBody  String endPoint( String endpoint) {

		XMLConfigImpl xmlConfigImpl;
		
		boolean process=true;
		String message="";
		try {
			
			SoapUIManager.settingEndpoint(endpoint);
		} catch (Exception e) {
			process=false;
			message= "error:"+e.getMessage();
		}
		
		if(process)
		{
			try {
				xmlConfigImpl = XMLConfigImpl.initial(context);
				xmlConfigImpl.setEndpoint(endpoint);
				xmlConfigImpl.saveDocument();
				message="success! server connects to endpoint:"+SoapUIManager.getUrl();
			} catch (DocumentException e) {				
				message= "error:"+e.getMessage();
			} catch (IOException e) {				
				message= "error:"+e.getMessage();
			}
		}
		
	    return message;         
	               
	   }
	
	@RequestMapping(value = "/config/uploadpath", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	   public @ResponseBody  String UploadPath( String uploadpath,String relationpath) {

		XMLConfigImpl xmlConfigImpl;
		try {
			xmlConfigImpl = XMLConfigImpl.initial(context);
			xmlConfigImpl.setUploadPath(uploadpath);
			xmlConfigImpl.setRelationPath(relationpath);
			xmlConfigImpl.saveDocument();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return   "nice";         
	               
	   }
	
	/**
	 * save user name and password
	 * @return
	 */
	@RequestMapping(value = "/rest/config/UserNamePassword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody void setUserNamePassword(String userName,String password) {
		
		try {
			XMLConfigImpl configImpl=XMLConfigImpl.initial(context);
			configImpl.setUserName(userName);
			configImpl.setPassword(password);
		
			configImpl.saveDocument();
		} catch (Exception e) {
			logger.error(" error");
		}
		
		
		
	} 


}
